/* 
 * File:   entity.cpp
 * Author: jolo
 * 
 * Created on: 10. Oktober 2011, 23:56
 * Lincense: New BSD License (The BSD 2-Clause License)
 */

#include "entity.hpp"

#include <tools/entity_formatter.hpp>

namespace model
{

    entity::entity()
    : boost::noncopyable(),
    uuid_(boost::uuids::random_generator()())
    {
    }

    entity::~entity()
    {
    }

    void entity::print_on(std::ostream& s) const
    {
        s << to_string();
    }

    const string entity::to_string() const
    {
        stringstream s;
        s << uuid();
        return s.str();
    }

    const string entity::short_label() const
    {
        stringstream s;
        s << uuid();
        return s.str();
    }

    string_vector entity::field_sequence() const
    {
        string_vector r;
        r.push_back("UUID");
        return r;
    }

    string_map entity::field_values() const
    {
        string_map r;
        r["UUID"] = to_string(uuid());
        return r;
    }

    const string entity::object_name() const
    {
        return "Entity";
    }

    const string entity::to_string(const int i) const
    {
        stringstream s;
        s << i;
        return s.str();
    }

    const string entity::to_string(const double d) const
    {
        stringstream s;
        s << d;
        return s.str();
    }

    const string entity::to_string(const boost::uuids::uuid u) const
    {
        stringstream s;
        s << u;
        return s.str();
    }

    const string entity::to_html_table() const
    {
        tools::entity_formatter f;
        string_vector rows;
        string_map vals = field_values();

        BOOST_FOREACH(string key, field_sequence())
        {
            rows.push_back(f.tag(f.tag(key, "td") + f.tag(vals[key], "td"), "tr"));
        }

        return f.make_table(rows);
    }

    const string entity::to_html_page() const
    {
        tools::entity_formatter f;
        return f.make_page(f.make_head(object_name()),
                           f.make_body(f.make_h1(object_name()) + to_html_table()));
    }
}
